<template>
	<uni-nav-bar fixed="true" backgroundColor="#f8f8f8" statusBar="true" border="false" left-icon="left" title="消息中心" @clickLeft="back"></uni-nav-bar>
	<view class="container">
		<view class="uni-padding-wrap">
		<view class="sticky bor_e8" :style="{'top':showNavTop+'px'}">
			<view><text :class="navIndex==0?'mr100 active':'mr100'" @click="checkIndex(0)">使用指南</text><text :class="navIndex==1?'active':''" @click="checkIndex(1)">更新公告</text></view>
		</view>
		</view>
		<view class="bottom-wrap">
		<scroll-view scroll-y="true" scroll-x="false" :style="{height:scrollH+'px'}" refresher-enabled="true" :refresher-triggered="isRefreshing" @refresherrefresh="onRefresh" @scrolltolower="onLoadMore">
		<view class="uni-padding-wrap">
		<!-- 内容切换 -->
		<view class="content" v-if="navIndex==0">
			<view class="vt50 tc" v-if="List.length == 0">
				<view>暂无数据</view>
			</view>
		<view class="" v-if="List.length>0">
			<view class="p300 bor_e8" v-for="item in List"  @tap="navgetTo('pages/my/news_info',item.id,'')">
				<view class="card-list is-flex">
					<image :src="item.coverUrl" mode="aspectFill"></image>
					<view class="card-item">
						<view class="color-black font_size_32 mb15">{{item.title}}</view>
						<view class="color-gray font_size_24 text-justify lineInfo">{{item.messageShortDesc}}</view>
					</view>
				</view>
			</view>
		</view>
		</view>
		<!-- 内容切换 -->
		<view class="content" v-if="navIndex==1">
			<view class="vt50 tc" v-if="List.length == 0">
				<view>暂无数据</view>
			</view>
			<view class="" v-if="List.length>0">
				<view class="p300 bor_e8" v-for="item in List" @tap="navgetTo('pages/my/news_info',item.id,'')">
					<view class="card-list is-flex">
						<image :src="item.coverUrl" mode="aspectFill"></image>
						<view class="card-item">
							<view class="color-black font_size_32 mb15">{{item.title}}</view>
							<view class="color-gray font_size_24 text-justify lineInfo">{{item.messageShortDesc}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		</view>
		<!-- 加载提示 -->
		 <view v-if="isLoading" class="tc mt100 mb100">{{loadMoreText}}</view>
		</scroll-view>
		</view>
	</view>
</template>

<script>
	import api from '@/common/api/webapi.js'; /*api*/
	export default {
		components: {
			
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				starValue:0,
				showNavTop:88,
				maxStar:5,
				single: "",
				keyword:'',
				navIndex:0,
				date: '全部时间',
				start: Date.now(),
				end: Date.now() + 180*24*3600*1000,
				scrollH:0,
				isRefreshing:false,
				isLoading:false,
				loadMoreText:"加载中...",
				List:[],
				param: {
					page: 1, //当前页
					limit: 10, //每页显示记录数
					PageCount: 0,
					keyword:'',
					startDate:'',
					endDate:'',
				},
			}
		},
		onReady() {
			// 获取可滚动区域的高度：视口高度 - tabbar切换卡高度就是剩余的内容可滚动的。
			//如果你使用取消了原生导航栏，记着把你自定义导航栏的高度也减掉哦
			uni.getSystemInfo({
				success: (res) => {
					this.scrollH = res.windowHeight - 149 - uni.upx2px(100)
				}
			})
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		watch: {

		},
		onShow() {
		},
		onLoad(option) {
			this.checkIndex(0);
			const app = getApp()
			let navTop = app.globalData.navTop;
			this.showNavTop = navTop + 44;
		},
		mounted(){
		},
		methods: {
			back(){
				uni.navigateBack();
			},
			checkIndex(index) {
				this.navIndex = index;
				// 重置分页和加载状态
				this.param.page = 1;
				this.isLoading = false;
				this.loadMoreText = "";
				if(this.navIndex == 0){
					this.getPageList('onPullDownRefresh',this.navIndex);
				}else if(this.navIndex == 1){
					this.getPageList('onPullDownRefresh',this.navIndex);
				}
			},
			dateChange: function(e) {
				this.date = e.detail.value;
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year;
				} else if (type === 'end') {
					if (month > 6) {
						year = year + 1;
					}
					year = year;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 城市选择器
			bindRegionChange: function(e) {
				this.region = e.detail.value;
			},
			// 星级点击事件
			starChange:function(e){
				this.starValue+1;
			},
			// 页面跳转
			navgetTo(page, param, type) {
				if (param != undefined) {
					type = type || '';
					let data = {
						data: param,
						page: type
					}
					uni.navigateTo({
						url: "/" + page + "?param=" + encodeURI(JSON.stringify(data))
					});
				} else {
					uni.navigateTo({
						url: "/" + page
					});
				}
			},
			onRefresh() {  // 下拉刷新
				this.isRefreshing = true;
				this.param.page = 1;
				this.getPageList('onPullDownRefresh',this.navIndex);
			},
			onLoadMore() {  // 上拉加载
				if (this.isLoading) return;
				this.isLoading = true;
				this.param.page = (this.param.page + 1);
				if (this.param.page > this.param.PageCount) {
					this.isLoading = true;
					this.loadMoreText = "--到底啦--"
					return;
				}
				this.getPageList('onPullDownRefresh',this.navIndex);
			},
			// 获取分页列表
			getPageList(type,navIndex) {
				let _this = this;
				if(navIndex == 0){
					let requestData = {
						params: { 
							messageType:'USE',
							page: _this.param.page, //当前页
							size: _this.param.limit, //每页显示记录数
						}
					}
								
					api.getMessage(requestData).then(res => {
						if (res.code === 0) {
							if (res.data.length > 0) {
								let pagelist = res.data;
								if (type == "onReachBottom") {
									_this.List = _this.List.concat(pagelist)
								} else {
									_this.List = pagelist;
								}
							}else{
								if(type == "onPullDownRefresh"){
									_this.List = [];
								}else if(type == "onReachBottom"){
									this.loadMoreText = "--到底啦--"
								}
							}
						} else {}
					_this.isRefreshing = false;
					}).catch(function(){
						_this.isRefreshing = false;
					})
				}else if(navIndex == 1){
					let requestData = {
						params: {
							messageType:'UPDATE',
							page: _this.param.page, //当前页
							size: _this.param.limit, //每页显示记录数
						}
					}
								
					api.getMessage(requestData).then(res => {
						if (res.code === 0) {
							if (res.data.length > 0) {
								let pagelist = res.data;
								_this.param.PageCount = Math.ceil(res.data.total / _this.param.limit);
								if (type == "onReachBottom") {
									_this.List = _this.List.concat(pagelist)
								} else {
									_this.List = pagelist;
								}
							}else{
								if(type == "onPullDownRefresh"){
									_this.List = [];
								}else if(type == "onReachBottom"){
									this.loadMoreText = "--到底啦--"
								}
							}
						} else {}
					_this.isRefreshing = false;
					_this.loadMoreText = "";
					}).catch(function(){
						_this.isRefreshing = false;
						_this.loadMoreText = "";
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		font-size: 14px;
		line-height: 24px;
	}

	.index-swiper {
		width: 100%;
		height: 670rpx;
	}

	.index-swiper image {
		width: 100%;
		height: 100%;
		object-fit: contain;
	}
	
	.bottom-wrap{
		position: relative;
		padding-top: 122rpx;
	}
	
	.is-flex{
		display: flex;
		align-items: center;
	}
	.flex1{
		flex: 1;
	}
	.mini-btn.p010{
		padding: 0 10px;
	}
	::v-deep.uni-searchbar__text-placeholder {
	    font-size: 12px;
	}
	::v-deep.uni-searchbar__box-search-input {
	    text-align: left;
	}
	.xf-wrap {
		position: relative;
		top: -60rpx;
		overflow: hidden;
	}

	.box-shadow {
		box-shadow: 2rpx 2px 2rpx $uni-text-color-grey8;
		background-color: #ffffff;
		border-radius: 20rpx;
	}

	.xf-wrap input {
		font-weight: bold;
	}

	.uni-flex {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.uni-row {
		flex-direction: row;
	}
	.sticky {
		height: 110rpx;
		line-height: 85rpx;
		background-color: #ffffff;
		width: 100%;
		color: #888;
		position: fixed;
		top: -1rpx;
		left: 0;
		right: 0;
		z-index: 999;
		text-align: center;
	}

	.sticky text {
		color: $uni-text-color-light-blue;
		font-weight: bold;
		padding: 10rpx 15rpx;
	}
	.sticky text.active{
		color: $uni-text-color-blue;
		border-bottom: 2px solid $uni-text-color-blue;
	}
	.card-list{
		image{
			width: 170rpx;
			height: 170rpx;
		}
		.card-item{
			flex: 1;
			margin-left: 25rpx;
		}
	}
	.pb130{
		padding-bottom: calc( 130rpx + max(env(safe-area-inset-bottom),20px));
	}
	.share-reward{
		.color-black{
			display: block;
			margin-bottom: 20rpx;
		}
		.is-flex{
			align-items: flex-start;
		}
		.color-gray{
			line-height: 1.8;
			text-align: justify;
			margin-left: 20rpx;
		}
	}
	
</style>